/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
	<div class="item-title">
	  <div class="subject">
	    <h3>调度日志</h3>
	  </div>
	</div>

    <div class="mDiv">
      <div class="ftitle">
        <h3>日志列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <div class="fbutton">
        <div class="add" @click="removeBatch()">
          <span>
            <i class="el-icon-plus" /> 批量删除
          </span>
        </div>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
      <div class="searchbox">
		<el-date-picker
		  v-model="dateRange"
		  type="daterange"
		  range-separator="至"
		  start-placeholder="起始时间"
		  end-placeholder="截止时间"
		  size="small"
		></el-date-picker>
        <el-input
          class="searchinput"
          v-model="job_id"
          width="10"
          size="small"
          placeholder="任务ID"
          clearable
        />
        <el-input
          class="searchinput"
          v-model="handler"
          width="10"
          size="small"
          placeholder="处理器"
          clearable
        />
        <el-button
          class="searchbtn"
          size="small"
          icon="el-icon-search"
          @click="currentPage = 1;getPage()"
        >搜索</el-button>
      </div>
    </div>
    <div class="tablebox">
      <el-table :data="tableData" ref="multipleTable" style="width: 100%;">
		<el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="任务ID" prop="job_id" width="80"></el-table-column>
		<el-table-column label="处理器" prop="handler" width="130"></el-table-column>
		<el-table-column label="触发类型" prop="trigger_type" width="80"></el-table-column>
		<el-table-column label="调度结果" prop="trigger_code" width="115"></el-table-column>
        <el-table-column label="调度备注" prop="trigger_msg" width="160" show-overflow-tooltip></el-table-column>
		<el-table-column label="失败次数" prop="fail_retry_count" width="90"></el-table-column>
        <el-table-column label="执行时间" width="140">
			<template slot-scope="scope">
				<span v-if="scope.row.create_time">{{(scope.row.create_time / 1000) | datefmtAll}}</span>
			</template>
		</el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
			  <el-button size="small" @click="remove(scope.row.id)" icon="el-icon-delete">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
	getJobLogPage,
	deleteJobLog,
} from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      pageSize: 10,
	  job_id: this.$route.params.id || null,
	  handler: '',
	  dialogFormVisible: false,
	  executeDialog: false,
	  form: {},
      isLoading: false,
	  param: null,
	  dateRange: null,
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPage();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPage();
    },
    getPage() {
      var that = this;
	  if (this.dateRange) {
	    var start_time = parseInt(this.dateRange[0].valueOf());
	    var end_time = parseInt(this.dateRange[1].valueOf()); + 86400000;
	  }
	  getJobLogPage({
		  start_time: start_time,
		  end_time: end_time,
		  job_id: this.job_id,
		  handler: this.handler,
		  page: this.currentPage,
		  size: this.pageSize
	  }).then(function(res) {
        that.tableData = res.records;
        that.totalCount = res.total;
      });
    },
    refresh() {
      this.getPage();
      this.$message.success("刷新成功！");
    },
	removeBatch() {
	  //获取选中的记录
	  var rows = this.$refs.multipleTable.selection;
	  var ids = [];
	  rows.forEach(element => {
	    ids.push(element.id);
	  });
	  if (ids.length == 0) {
	    this.$message.info({
	      message: "请选择需要删除的日志!"
	    });
	    return;
	  }
	  this.remove(ids);
	},
	remove(ids) {
	  var that = this;
	  this.$confirm("确认删除?", "提示", {
	    confirmButtonText: "确定",
	    cancelButtonText: "取消",
	    type: "warning"
	  })
	    .then(() => {
	      deleteJobLog(ids).then(function(res) {
	        if (res.status == 1) {
	          that.getPage();
	          that.$message({
	            type: "success",
	            message: "删除成功!"
	          });
	        } else {
	          that.$message.error({
	            message: res.msg
	          });
	        }
	      });
	    })
	    .catch(() => {
	      that.$message.info({
	        message: "已取消删除"
	      });
	    });
	}
  }
};
</script>

<style scoped>
.searchinput {
  width: auto;
}
</style>
